//
//  LiveListTVC.m
//  Stream
//
//  Created by shaw on 16/7/28.
//  Copyright © 2016年 showshaw. All rights reserved.
//
//  直播列表

#import "LiveListTVC.h"
#import "LiveItem.h"
#import <AVFoundation/AVFoundation.h>
#import "MovieViewController.h"

@interface LiveListTVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView* tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation LiveListTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"直播列表";
    
    [self setupView];
    
    //
    [self loadData];
}
//加载数据
-(void)loadData
{
    NSString * apiURL = [Utils BaseUrlOfApi:@"live-list"];
    
    // mapping
    [LiveItem mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                    @"ID" : @"id",
                    @"title" : @"name",
                    @"url" : @"address"
                 };
    }];
    
    [XXNetRequest POST:apiURL
                params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                    if ([responseObject[@"code"] integerValue] == NetRequestStatusSuccess) {
                        NSArray * data = responseObject[@"result"];
                        self.dataArray = [LiveItem mj_objectArrayWithKeyValuesArray:data];
                        [self.tableView reloadData];
                    }
                } fail:^(NSURLSessionDataTask *task, NSError *error) {
                    
                }];
     
}

-(void)setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 50.0f;
    [self.view addSubview:_tableView];
    
}
#pragma mark - init tableview

-(NSMutableArray*)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
//
//        NSString * file = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"live" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
//        
//        NSArray* str = [file componentsSeparatedByString:@"\n"];
//        [str enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            NSArray * objectArrray = [obj componentsSeparatedByString:@","];
//            LiveItem * item = [LiveItem alloc];
//            item.title = objectArrray[0];
//            item.url = objectArrray[1];
//            [_dataArray addObject:item];
//        }];
    }
    return _dataArray;
}

#pragma mark - TableView Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString* identity = @"identity";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identity];
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:identity];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    LiveItem * item = self.dataArray[indexPath.row];
    cell.textLabel.text = item.title;
    return cell;
}
//点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LiveItem * item = self.dataArray[indexPath.row];
    
    MovieViewController * movieVC = [MovieViewController new];
    movieVC.playItem = item;
    
//    [self.navigationController pushViewController:movieVC animated:YES];
//    [self showDetailViewController:movieVC sender:nil];
    [self presentViewController:movieVC animated:YES completion:^{
    }];
}

@end
